/**
 * Cs340 Fatboy Mafia Team
 */
package fatboy.hypeerweb;

import junit.framework.Assert;

/**
 * The Edge-Node State.  
 * An edge node must have a list of surrogate pointers.
 * An edge node must have a parent which is an interior node.  Whenever a node gets a new parent, it becomes an edge node.
 * An edge node has no children.
 * (An edge node will have a height that is greater than the height of the "complete" hypercube.)
 * 
 * @author Ilya Vinogradov
 * 
 * @domain a singleton instance
 */
class EdgeNodeState extends NodeState {

	/* (non-Javadoc)
	 * @see fatboy.hypeerweb.NodeState#findInsertionPoint()
	 */
	public Node findInsertionPoint(Node thisNode) {
		
		Node surface = getSurfaceOrLastNode(thisNode);

		Node lower = surface;
		Node edgeUpper = thisNode;
		
		return surface.findFirstChildlessInRange(lower, edgeUpper);
	}
	
	protected Node getSurfaceOrLastNode(Node thisNode) {
		Node surrogate = null;
		
		surrogate = thisNode.getPointers().get(0).getNode();

		/*
		System.out.println("this edgenode : " + thisNode);
		try {
			Thread.currentThread().sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		*/
		return surrogate.getNodeState().getSurfaceOrLastNode(surrogate);
	}


	public boolean hasEdgeChild(Node thisNode) {
		Assert.assertSame(thisNode.getChild(), Node.nullNode);
		return false;
	}


	public boolean hasParent(Node thisNode) {
		Assert.assertNotSame(thisNode.getParent(), Node.nullNode);
		return true;
	}

	public NodeAddress getFirstUpDownNodeAddress(Node thisNode) {
		return thisNode.getPointers().get(0).getNode().getAddress();
	}
	
	/* (non-Javadoc)
	 * @see fatboy.hypeerweb.NodeState#getType()
	 */
	protected NodeType getType() {
		return NodeType.EDGE_NODE;
	}

	@Override
	public	void assertNodeStateIsCorrect(Node thisNode) {
		Assert.assertTrue(!(thisNode.getPointers().isEmpty()));
		Assert.assertEquals(thisNode.getParent().getNodeType(), NodeType.INTERIOR_NODE);
		Assert.assertEquals(thisNode.getChild().getNodeType(), NodeType.NULL_NODE);
	}
}
